class ChecklistsController < ApplicationController
  before_filter :except => [:get_before, :get_after] do |controller|
    controller.authorize_permissions({:blocked_permissions => (User::PermissionLevels - ["admin"])})
  end

  def get_before
    @rules = @current_user.courier.checklist_recurrence_rules
    if @rules != nil then
       # I'm a horrible person now
      @tasks = @rules.map {|x| 
        x.checklist
      }.find_all{|list| 
        list.checklist_type == "before"
      }.map{|x| 
        x.checklist_items
      }.flatten
    end
    
    respond_to do |format|
      format.json  { render :json => @tasks }
    end
  end  
  
  def get_after
    @rules = @current_user.courier.checklist_recurrence_rules
    if @rules != nil then
       # I'm a horrible person now
      @tasks = @rules.map {|x| x.checklist}.find_all{|list| list.checklist_type == "after"}.map{|x| x.checklist_items}.flatten
    end
    
    respond_to do |format|
      format.json  { render :json => @tasks }
    end
  end

  # GET /checklists
  # GET /checklists.json
  def index
	@title = "View Checklists"
  
	session[:show_hidden_checklists] ||= false
	@show_hidden = session[:show_hidden_checklists]
	
	if @show_hidden
	  @checklists = Checklist.all
	else
	  @checklists = Checklist.find_all_by_hidden false
	end
	
	@page_results = @checklists.paginate(:page => params[:page], :per_page => 10)
	
    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @checklists }
    end
  end

  # GET /checklists/1
  # GET /checklists/1.json
  def show
    @checklist = Checklist.find(params[:id])
	@title = "View Checklist: " + @checklist.name

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @checklist }
    end
  end

  # GET /checklists/new
  # GET /checklists/new.json
  def new
    @title = "New Checklist"
    @checklist = Checklist.new
	@checklist.checklist_items << ChecklistItem.new
	
    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @checklist }
    end
  end

  # GET /checklists/1/edit
  def edit
    @checklist = Checklist.find(params[:id])
	@checklist_recurrence_rule = @checklist.checklist_recurrence_rules.new(
	  :days => (0..6).inject {|a,e| "#{a},#{e}"})
	@unassigned_couriers = Courier.all - @checklist.checklist_recurrence_rules.map {|x| x.courier}
	@title = "Edit Checklist: " + @checklist.name
  end

  # POST /checklists
  # POST /checklists.json
  def create
    @checklist = Checklist.new(params[:checklist])

	@checklist.checklist_items.each_with_index { |c,i|
		c.item_number = i + 1
	}
	
    respond_to do |format|
      if @checklist.save
        flash[:notice] = 'Checklist was successfully created.'
        format.html { redirect_to edit_checklist_path @checklist }
        format.json  { render :json => @checklist, :status => :created, :location => @checklist }
      else
		@title = "New Checklist"
        format.html { render :action => "new" }
        format.json  { render :json => @checklist.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /checklists/1
  # PUT /checklists/1.json
  def update
    @checklist = Checklist.find(params[:id])

    respond_to do |format|
      if @checklist.update_attributes(params[:checklist])
        flash[:notice] = 'Checklist was successfully updated.'
        format.html { redirect_to(@checklist) }
        format.json  { head :ok }
      else
		@title = "Edit Checklist: " + @checklist.name
        format.html { render :action => "edit" }
        format.json  { render :json => @checklist.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /checklists/1
  # DELETE /checklists/1.json
  def destroy
    @checklist = Checklist.find(params[:id])
    @checklist.hidden = !@checklist.hidden

	if @checklist.save
	  flash[:notice] = @checklist.hidden ? 'Checklist hidden' : 'Checklist un-hidden'
	else
	  flash[:notice] = @checklist.hidden ? "Checklist couoldn't be hidden" : "Checklist couldn't be un-hidden"
	end
	
    respond_to do |format|
      format.html { redirect_to(checklists_url) }
      format.json  { head :ok }
    end
  end
  
  def toggle_hidden
    session[:show_hidden_checklists] ||= false
	session[:show_hidden_checklists] = !session[:show_hidden_checklists]
	
	redirect_to :back
  end
  
  def create_rule
    @checklist_recurrence_rule = ChecklistRecurrenceRule.new(params[:checklist_recurrence_rule])
	
	@checklist = @checklist_recurrence_rule.checklist
	
	if @checklist_recurrence_rule.save
	  flash[:notice] = 'Courier succesfully assigned to checklist.'
	  redirect_to edit_checklist_path(@checklist)
	else
	  redirect_to edit_checklist_path(@checklist)
	end
  end
  
  def destroy_rule
    @checklist_recurrence_rule = ChecklistRecurrenceRule.find(params[:rule_id])
	
	@checklist = @checklist_recurrence_rule.checklist
	@checklist_recurrence_rule.destroy
	flash[:notice] = 'Courier unassigned from checklist.'
	
	redirect_to edit_checklist_path(@checklist)
  end
end
